package com.yixun.config;

import com.yixun.service.RedisService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.FileSystemUtils;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@Configuration
@EnableScheduling
public class SaticScheduleTask {
    @Value("${file.redis_key}")
    public static  String FILES_TO_BE_DELETED;
    @Resource
    private RedisService redisService;
    @Value("${file.uploadFile}")
    private static String FILE_PATH;
    @Scheduled(cron = "0 59 2 ? * FRI ")
    private void configureTasks() {
        /*删除已经上传的文件 但是最终没有提交的文件*/
        List<Object> list = redisService.getFilesToBeDeleted(FILES_TO_BE_DELETED);
        for (Object value : list) {
            System.out.println(FILE_PATH+value);
            boolean b = FileSystemUtils.deleteRecursively(new File(FILE_PATH + value));
            if (b) {
                redisService.deleteValueOfKey("filesToBeDeleted", (String) value);
            }
        }
    }
}
